﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using JKF.DataModule.IDAL;
using JKF.Substructure;
using System.Data.SqlClient;//请先添加引用
namespace JKF.DataModule.SqlServerDAL
{
    /// <summary>
    /// 数据访问类dalSYS_MODULE。
    /// </summary>
    public partial class dalSYS_MODULE:IdalSYS_MODULE
    {
        private const string FieldShow = "[MODULE_KEY] ,[MODULE_CODE],[MODULE_NAME],[MODULE_NAME_EN],[URL],[URL_PIC_SMALL],[URL_PIC_LARGE] ,isnull([DISPLAY_ORDER],0) as DISPLAY_ORDER,[REMARK] ,CONVERT(CHAR(10),DATE_CREATE,120) DATE_CREATE,CONVERT(CHAR(10),DATE_UPDATE,120) DATE_UPDATE";
        private const string FieldOrder = "MODULE_CODE,DISPLAY_ORDER";
        private const string FieldKey = "MODULE_KEY";
        /// <summary>
        /// 获取系统模块列表
        /// </summary>
        /// <param name="MODULE_NAME">模块名称</param>
        /// <param name="DATE_START">开始日期</param>
        /// <param name="DATE_END">结束日期</param>
        /// <param name="pageCurrent">当前页码</param>
        /// <param name="pageSize">显示多少</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总行数</param>
        /// <returns></returns>
        public static DataTable GetModules(string MODULE_NAME, string DATE_START, string DATE_END,
            int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            var sql = new StringBuilder(@"  1=1 ");
            if (!string.IsNullOrEmpty(MODULE_NAME))
            {
                sql.AppendFormat(" AND (MODULE_NAME  LIKE '%{0}%' OR  MODULE_NAME_EN  LIKE '%{0}%')", MODULE_NAME);
            }

            if (!string.IsNullOrEmpty(DATE_START))
            {
                sql.AppendFormat(" and DATE_CREATE >'{0}'", DATE_START);
            }
            if (!string.IsNullOrEmpty(DATE_END))
            {
                sql.AppendFormat(" and DATE_CREATE <'{0}'", DATE_END);
            }

            return DataPage.GetPageInfo("SYS_MODULE", FieldKey, pageCurrent, pageSize, FieldShow, FieldOrder, sql.ToString(), ref pageCount, ref rowCount).Tables[0];
        }

        /// <summary>
        /// 获取全部模块
        /// </summary>
        /// <returns></returns>
        public static DataTable GetSysModuleDAL()
        {
            string sql = "SELECT MODULE_CODE,MODULE_NAME FROM dbo.SYS_MODULE ORDER BY MODULE_CODE";
            return DB.HR.ExecuteDataSet(CommandType.Text, sql).Tables[0];
        }
        /// <summary>
        /// 获取模块表与操作表中的数据，组装成了树形结构
        /// </summary>
        /// <returns></returns>
        public static DataTable GetSysModule_SysActionDAL()
        {
            Database db = DB.HR;
            DbCommand dbCommand = db.GetStoredProcCommand("[dbo].[proc_Module_Action_ToTree]");
            DataSet ds = db.ExecuteDataSet(dbCommand);
            return ds.Tables[0];
        }
    }
}
